package com.xyy.saas.payment.util;

import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.service.PayAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import util.AESUtil;

import java.util.Map;

/**
 * @Author: wuqiongda
 * @Date: 2020/4/13 13:35
 */
@Service
@DependsOn("apolloUtil")
public class TokenUtil {

    public String tokens = ApolloUtil.getProperty(Constants.WARE_ENCRYPTION);
    @Autowired
    private PayAccountService payAccountService;

    public String checkUserToken(String userToken, String businessIdType) throws Exception {
        if (userToken==null){
            return null;
        }
        String accountId;
        //验证token
        Map<String, String> tokenMap = JSONUtils.parse2Map(tokens, String.class, String.class);
        accountId = AESUtil.decrypt(userToken, tokenMap.get(businessIdType));
        if (accountId == null){
           return null;
        }
        //验证id是否存在
        if (payAccountService.getPayAccountByAccountId(accountId)!=null){
            return accountId;
        }
        return null;
    }
}
